<?php

namespace App\Services;

use App\Utils\Http;

class UserService extends BaseService {

    /**
     * 通过wxcode
    *获取accessToken
     */
    public static function getAccessToken($wxcode, $redirectUri, $env){
        if($env){
            $accessTokenUrl = "https://open.wecard.qq.com/connect/oauth2/token";
        }else{
            $accessTokenUrl = "https://tygate.weixiao.qq.com/cgi-bin/oauth2/token";
        }
        $service = new UserService();
        $arr = [
            'wxcode' => $wxcode,
            'app_key' => $service->appKey,
            'app_secret' => $service->appSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $redirectUri
        ];

        $res = Http::post($accessTokenUrl, $arr);
        return json_decode($res, true)['access_token'];
    }

    /**
    *获取用户信息
     */
    public static function getUserInfo($accessToken, $env){
        if($env){
            $userUrl = "https://open.wecard.qq.com/connect/oauth/get-user-info";
        }else{
            $userUrl = "https://tygate.weixiao.qq.com/connect/oauth/get-user-info";
        }
        $arr = [
            'access_token' => $accessToken
        ];
        $res = Http::post($userUrl, $arr);

        return json_decode($res, true);
    }

}
